#include "camera_driver/camera.hpp"

#include <stdexcept>

#include "camera_driver/hikrobot.hpp"
#include "camera_driver/mindvision.hpp"
namespace io
{
  Camera::Camera(const io::CameraParams &camera_param, const rclcpp::Logger &logger) : logger_(logger)
  {

    if (camera_param.camera_name == "mindvision")
    {
      camera_ = std::make_unique<MindVision>(camera_param.exposure_ms, camera_param.gamma, camera_param.vid_pid, logger);
    }

    else if (camera_param.camera_name == "hikrobot")
    {
      camera_ = std::make_unique<HikRobot>(camera_param.exposure_ms, camera_param.gain, camera_param.vid_pid, logger);
    }

    else
    {
      throw std::runtime_error("Unknow camera_name: " + camera_param.camera_name + "!");
    }
  }

  void Camera::read(sensor_msgs::msg::Image &img)
  {
    if (camera_) camera_->read(img);
  }
  void Camera::close()
  {
    if (camera_) camera_->close();
  }
  bool Camera::open()
  {
    if (!camera_) return false;
    return camera_->open();
  }
  bool Camera::set_param(const std::string &param_name, double value)
  {
    if (!camera_) return false;
    return camera_->set_param(param_name, value);
  }
}